package com.dearbinge.opentapi.content.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserContactService;
import com.dearbinge.data.pojo.im.ImUserContact;
import com.dearbinge.data.pojo.im.vo.ImUserContactVO;
import com.dearbinge.utils.ResultMsg;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 好友列表控制层
 *
 * @author wes
 * @date 2019/2/27
 */
@Api(tags = "5.1、通讯-用户好友列表")
@RestController
@RequestMapping("/userContact")
@CrossOrigin
@EnableAutoConfiguration
public class UserContactController extends WebMvcConfigurerAdapter {

    @Autowired
    private UserContactService userContactService;



    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = userContactService.delete(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 新增/修改
     */
    @ApiOperation(value = "新增/修改",notes = "新增/修改")
    @RequestMapping(value = "addOrUpdate",method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdate(@RequestBody ImUserContact userContact){
        try {
            if (userContact.getId() != null && userContact.getId() != 0){
                Integer dataCode = userContactService.update(userContact);
                if (dataCode > 0){
                    return ResultMsg.ok("修改成功");
                }
            }else{
                Integer dataCode = userContactService.insert(userContact);
                if (dataCode > 0){
                    return ResultMsg.ok("新增成功");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "id",paramType = "query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public ResultMsg<ImUserContact> getById(Integer id){
        try {
            ImUserContact userContact = userContactService.getById(id);
            return ResultMsg.ok(userContact);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 根据用户ID查询好友列表 分页
     */
    @ApiOperation(value = "根据用户ID查询全部",notes = "根据ID查询")
    @ApiResponses({
            @ApiResponse(response = ImUserContactVO.class, code = 200, message = "返回结果参数")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query",required=true),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")})
    @RequestMapping(value = "/listByUserId",method = RequestMethod.GET)
    public ResultMsg<Page> listByUserId(Integer userId,Integer current, Integer size){
        try {
            Page userApply = userContactService.listByUserId(userId,current,size);
            return ResultMsg.ok(userApply);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
